package com.sduept.nwld.dataserver.rest;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.bigdata.fault.protection.model.ProtectionActionInfoDTO;
import com.sduept.bigdata.fault.protection.service.ProtectionActionQueryManager;
import com.sduept.bigdata.risk.service.RiskAlarmMessageQueryManager;
import com.sduept.bigdata.risk.service.RiskAlarmMessageQueryManager.AffectedObject;
import com.sduept.bigdata.weather.map.MapDataCache;
import com.sduept.cim.model.core.Substation;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.core.entity.SystemParameter;
import com.sduept.message.entity.AlarmMessage;
import com.sduept.utils.DateUtil;


/**
 * 风险预警REST服务
 */
@RestController
@RequestMapping("/riskwarning/")
public class RiskWarningServices {

	@Autowired
	private SuperDAO dao;
	@Autowired
	private MapDataCache cache;
	@Autowired
	private RiskAlarmMessageQueryManager ram;
	@Autowired
	private ParamManager pc;
	@Autowired
	private MapDataCache cc;
	@Autowired
	ProtectionActionQueryManager protectionActionQueryManager;

	/**
	 * 获取一次设备风险预警站点
	 * 
	 * @return
	 */
	
	@GetMapping("/getPrimaryRiskStations")
	public String getPrimaryStations() {
		JSONArray rtn = new JSONArray();
		Random r = new Random();
		List<Substation> st = cache.getS500List().subList(3, 17);
		for (Substation s : st) {
			JSONObject jo = new JSONObject();
			int level = r.nextInt(100);
			jo.put("station", s);
			jo.put("level", level);
			jo.put("style", getColorY2R(level));
			rtn.add(jo);
		}
		// TODO：测试数据
		return rtn.toJSONString();
	}

	private static String getColorY2R(int v) {
		// String y = "#ffff00";
		// String r = "#ff0000";
		int ic = 255 * v / 100;
		String c = String.format("%02x", 255 - ic);
		return "#ff" + c + "00";

	}

	/**
	 * 获取二次设备风险预警站点
	 * 
	 * @return
	 */
	
	@GetMapping("/getSecondaryRiskStations")
	
	public String getSecondaryRiskStations() {
		// TODO：测试数据
		JSONArray rtn = new JSONArray();
		Random r = new Random();
		List<Substation> st = cache.getS500List().subList(17, 27);
		for (Substation s : st) {
			JSONObject jo = new JSONObject();
			int level = r.nextInt(100);
			jo.put("station", s);
			jo.put("level", level);
			jo.put("style", getColorY2R(level));
			rtn.add(jo);
		}
		// TODO：测试数据
		return rtn.toJSONString();
	}

	/**
	 * 获取变电站断路器熄弧时间
	 * 
	 * @return
	 */
	
	@GetMapping("/getBreakerArcTimesByStationId/{sid}")
	public String getBreakerArcTimesByStationId(@PathVariable("sid") String sid) {

		int max = 7;
		int min = 5;
		Random random = new Random();
		String sql = "select  name from t_cim_breaker t where t.sub_id='" + sid + "' and t.standVoltagelevel='500'";

		List<?> breakers = dao.nativeQuery(sql);
		JSONArray josns = new JSONArray();
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (Object o : breakers) {
			JSONObject jo = new JSONObject();
			String bname = o.toString();
			jo.put("name", bname);
			JSONArray vs = new JSONArray();
			TreeSet<Date> ds = new TreeSet<>();
			for (int i = 0; i < 6; i++) {
				Date d = DateUtil.randomDate("2014-01-01", "2016-12-31");
				ds.add(d);
			}

			for (Date d : ds) {
				int iv = random.nextInt(max) % (max - min + 1) + min;
				float v = Float.parseFloat(dcmFmt.format(iv + random.nextFloat()));
				vs.add(new Object[] { bname, DateUtil.getStringDate(d, DateUtil.pattern_ymdhms), v });
			}

			jo.put("value", vs);
			josns.add(jo);
		}
		return josns.toJSONString();
	}

	
	@GetMapping("/getBreakerArcTimes")
	public String getBreakerArcTimes() {
		int max = 7;
		int min = 5;
		Random random = new Random();
		String sql = "select sub_name,name from t_cim_breaker t where t.standVoltagelevel='500'";
		List<?> breakers = dao.nativeQuery(sql);
		JSONArray josns = new JSONArray();
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (Object o : breakers) {
			JSONObject jo = new JSONObject();
			Object[] os = (Object[]) o;
			String subname = os[0].toString();
			String bname = os[1].toString();
			jo.put("name", bname);
			JSONArray vs = new JSONArray();
			TreeSet<Date> ds = new TreeSet<>();
			for (int i = 0; i < 4; i++) {
				Date d = DateUtil.randomDate("2014-01-01", "2016-12-31");
				ds.add(d);
			}
			DateTime dt;
			for (Date d : ds) {
				int iv = random.nextInt(max) % (max - min + 1) + min;
				float v = Float.parseFloat(dcmFmt.format(iv + random.nextFloat()));
				dt = new DateTime(d.getTime());
				vs.add(new Object[] { subname, bname, dt.toString("yyyy-MM-dd HH:mm:ss"), v });
			}

			jo.put("value", vs);
			josns.add(jo);
		}
		return josns.toJSONString();
	}

	/**
	 * 获取一个断路器的熄弧时间
	 * 
	 * @return
	 */
	
	@GetMapping("/getBreakerArcTimesById/{bid}")
	public String getBreakerArcTimesById(@PathVariable("bid") String bid) {

		int max = 7;
		int min = 2;
		Random random = new Random();
		String s1 = "select name from t_cim_breaker t where t.id='" + bid + "';";
		List<?> breakers = dao.nativeQuery(s1);
		JSONArray josns = new JSONArray();
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (Object o : breakers) {
			JSONObject jo = new JSONObject();
			String bname = o.toString();
			jo.put("name", bname);
			JSONArray vs = new JSONArray();
			TreeSet<Date> ds = new TreeSet<>();

			for (int i = 0; i < 6; i++) {
				Date d = DateUtil.randomDate("2014-01-01", "2016-12-31");
				ds.add(d);
			}

			for (Date d : ds) {
				int iv = random.nextInt(max) % (max - min + 1) + min;
				float v = Float.parseFloat(dcmFmt.format(iv + random.nextFloat()));
				vs.add(new Object[] { DateUtil.getStringDate(d, DateUtil.pattern_ymdhms), v });
			}
			jo.put("value", vs);
			josns.add(jo);
		}
		return josns.toJSONString();
	}

	/**
	 * 获取断路器动作时间
	 * 
	 * @return
	 */
	
	@GetMapping("/getBreakerActionTimesByStationId/{sid}")
	public String getBreakerActionTimesByStationId(@PathVariable("sid") String sid) {
		String s1 = "";
		int max = 40;
		int min = 30;
		Random random = new Random();
		if (sid.equals("空")) {
			s1 = "select name from t_cim_breaker t where standVoltagelevel='500';";
		} else {
			s1 = "select name from t_cim_breaker t where t.sub_id='" + sid + "' and standVoltagelevel='500';";
		}
		List<?> breakers = dao.nativeQuery(s1);
		JSONArray josns = new JSONArray();
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (Object o : breakers) {
			JSONObject jo = new JSONObject();
			String bname = o.toString();
			jo.put("name", bname);
			JSONArray vs = new JSONArray();
			TreeSet<Date> ds = new TreeSet<>();
			for (int i = 0; i < 6; i++) {
				Date d = DateUtil.randomDate("2014-01-01", "2016-12-31");
				ds.add(d);
			}

			for (Date d : ds) {
				int iv = random.nextInt(max) % (max - min + 1) + min;
				float v = Float.parseFloat(dcmFmt.format(iv + random.nextFloat()));
				vs.add(new Object[] { DateUtil.getStringDate(d, DateUtil.pattern_ymdhms), v });
			}
			jo.put("value", vs);
			josns.add(jo);
		}
		return josns.toJSONString();
	}


	/**
	 * 获取一个断路器动作时间
	 * 
	 * @return
	 */

	@GetMapping("/getBreakerActionTimesById/{bid}")
	public String getBreakerActionTimesById(@PathVariable("bid") String bid) {

		int max = 40;
		int min = 30;
		Random random = new Random();
		String s1 = "select name from t_cim_breaker t where t.id='" + bid + "' ;";
		List<?> breakers = dao.nativeQuery(s1);
		JSONArray josns = new JSONArray();
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (Object o : breakers) {
			JSONObject jo = new JSONObject();
			String bname = o.toString();
			jo.put("name", bname);
			JSONArray vs = new JSONArray();
			for (int i = 0; i < 6; i++) {
				Date d = DateUtil.randomDate("2014-01-01", "2016-12-31");
				int iv = random.nextInt(max) % (max - min + 1) + min;
				float v = Float.parseFloat(dcmFmt.format(iv + random.nextFloat()));
				vs.add(new Object[] { DateUtil.getStringDate(d, DateUtil.pattern_ymdhms), v });
			}
			jo.put("value", vs);
			josns.add(jo);
		}
		return josns.toJSONString();
	}

	/**
	 * 获取变电站保护动作时间
	 * 
	 * @return
	 */
	@GetMapping("/getProtectActTimesByStationId/{sid}")
	public List<ProtectionActionInfoDTO> getProtectActTimesByStationId(@PathVariable("sid") String sid) {
		String[] strings = {sid};
		//调用方法
		List<ProtectionActionInfoDTO> protectionActionTimeDtoByStationId =
				protectionActionQueryManager.getProtectionActionTimeDtoByStationId(strings,null,null);

		JSONArray jsonArray = new JSONArray();
		for (ProtectionActionInfoDTO o : protectionActionTimeDtoByStationId) {
			JSONObject json = new JSONObject();
			json.put("stationId", o.getStationId());
			json.put("distance0MomentsTime", o.getDistance0MomentsTime());
			jsonArray.add(json);
		}
		return protectionActionTimeDtoByStationId;
	}

	//	@GetMapping("/getProtectActTimesByStationId2/{sid}")
//	public String getProtectActTimesByStationId2(@PathVariable("sid") String sid) {
//		JSONArray rtn = new JSONArray();
//		String sqlStr = "select t.mProtectName from t_bx_protection_equipment t where t.cim_station_id='" + sid
//				+ "' and mPrimdevID like";
//		String xlusql = sqlStr + " '%XL%'";
//		String byqsql = sqlStr + " '%BYQ%'";
//		String mxsql = sqlStr + " '%MX%'";
//		String kgsql = sqlStr + " '%KG%'";
//		JSONObject xl = this.getJSONObject(xlusql, "线路保护");
//		JSONObject byq = this.getJSONObject(byqsql, "变压器保护");
//		JSONObject mx = this.getJSONObject(mxsql, "母线保护");
//		JSONObject kg = this.getJSONObject(kgsql, "开关保护");
//		rtn.add(xl);
//		rtn.add(byq);
//		rtn.add(mx);
//		rtn.add(kg);
//		return rtn.toJSONString();
//	}

	public JSONObject getJSONObject(String sql, String type) {
		int max = 15;
		int min = 10;
		Random random = new Random();
		JSONObject xl = new JSONObject();
		List<?> xlprotects = dao.nativeQuery(sql);
		JSONArray josns = new JSONArray();
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (Object o : xlprotects) {
			JSONObject jo = new JSONObject();
			String pname = o.toString();
			jo.put("name", pname);
			JSONArray vs = new JSONArray();
			TreeSet<Date> ds = new TreeSet<>();
			for (int i = 0; i < 6; i++) {
				Date d = DateUtil.randomDate("2014-01-01", "2016-12-31");
				ds.add(d);
			}
			for (Date d : ds) {
				int iv = random.nextInt(max) % (max - min + 1) + min;
				float v = Float.parseFloat(dcmFmt.format(iv + random.nextFloat()));
				vs.add(new Object[] { DateUtil.getStringDate(d, DateUtil.pattern_ymdhms), v });
			}
			jo.put("time", vs);
			josns.add(jo);
			xl.put("value", josns);
			xl.put("type", type);
		}

		return xl;
	}

	/**
	 * 获取一个保护的动作时间
	 * 
	 * @return
	 */

	@GetMapping("/getProtectActTimes")
	public String getProtectActTimes() {
		int max = 20;
		int min = 10;
		Random random = new Random();
		JSONArray josns = new JSONArray();
		JSONArray vs = new JSONArray();
		TreeSet<Date> ds = new TreeSet<>();
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (int i = 0; i < 6; i++) {
			Date d = DateUtil.randomDate("2014-01-01", "2016-12-31");
			ds.add(d);
		}
		for (Date d : ds) {
			int iv = random.nextInt(max) % (max - min + 1) + min;
			float v = Float.parseFloat(dcmFmt.format(iv + random.nextFloat()));
			vs.add(new Object[] { DateUtil.getStringDate(d, DateUtil.pattern_ymdhms), v });
		}
		josns.add(vs);

		return josns.toJSONString();
	}

	/**
	 * 获取变电站保护故障切除时间
	 * 
	 * @return
	 */

	@GetMapping("/getFaultRemoveTimeByStationId/{sid}")
	/*
	 * public String getFaultRemoveTimeByStationId(@PathVariable("sid") String sid) {
	 * JSONArray rtn = new JSONArray(); String sqlStr=
	 * "select t.mProtectName from t_bx_protection_equipment t where t.cim_station_id='"
	 * +sid+"' and mPrimdevID like"; String xlusql = sqlStr+" '%XL%'"; String
	 * byqsql=sqlStr+" '%BYQ%'"; String mxsql = sqlStr+" '%MX%'"; String
	 * kgsql=sqlStr+" '%KG%'"; JSONObject xl =this.getTime(xlusql,"线路保护");
	 * JSONObject byq =this.getTime(byqsql,"变压器保护"); JSONObject mx
	 * =this.getTime(mxsql,"母线保护"); JSONObject kg =this.getTime(kgsql,"开关保护");
	 * rtn.add(xl); rtn.add(byq); rtn.add(mx); rtn.add(kg); return
	 * rtn.toJSONString(); }
	 * 
	 * public JSONObject getTime(String sql,String type){ int max=15; int min=10;
	 * Random random = new Random(); JSONObject xl = new JSONObject(); List<?>
	 * xlprotects = dao.nativeQuery(sql); JSONArray josns = new JSONArray();
	 * for(Object o : xlprotects){ JSONObject jo = new JSONObject(); String pname =
	 * o.toString(); jo.put("name", pname); int iv = random.nextInt(max)%(max-min+1)
	 * + min; float v = iv+random.nextFloat(); jo.put("time",v); josns.add(jo);
	 * xl.put("value", josns); xl.put("type", type); } return xl; }
	 */
	public String getFaultRemoveTimeByStationId(@PathVariable("sid") String sid) {
		JSONArray rtn = new JSONArray();
		String sqlStr = "select t.mProtectName from t_bx_protection_equipment t where t.cim_station_id='" + sid
				+ "' and mPrimdevID like";
		String xlusql = sqlStr + " '%XL%'";
		String byqsql = sqlStr + " '%BYQ%'";
		String mxsql = sqlStr + " '%MX%'";
		String kgsql = sqlStr + " '%KG%'";
		JSONObject xl = this.getTime(xlusql, "线路保护");
		JSONObject byq = this.getTime(byqsql, "变压器保护");
		JSONObject mx = this.getTime(mxsql, "母线保护");
		JSONObject kg = this.getTime(kgsql, "开关保护");
		rtn.add(xl);
		rtn.add(byq);
		rtn.add(mx);
		rtn.add(kg);
		return rtn.toJSONString();
	}

	public JSONObject getTime(String sql, String type) {
		int max = 15;
		int min = 10;
		Random random = new Random();
		JSONObject xl = new JSONObject();
		List<?> xlprotects = dao.nativeQuery(sql);
		JSONArray josns = new JSONArray();
		DecimalFormat dcmFmt = new DecimalFormat("0.00");
		for (Object o : xlprotects) {
			JSONObject jo = new JSONObject();
			String pname = o.toString();
			jo.put("name", pname);
			JSONArray vs = new JSONArray();
			TreeSet<Date> ds = new TreeSet<>();
			for (int i = 0; i < 6; i++) {
				Date d = DateUtil.randomDate("2014-01-01", "2016-12-31");
				ds.add(d);
			}
			for (Date d : ds) {
				int iv = random.nextInt(max) % (max - min + 1) + min;
				float v = Float.parseFloat(dcmFmt.format(iv + random.nextFloat()));
				vs.add(new Object[] { DateUtil.getStringDate(d, DateUtil.pattern_ymdhms), v });
			}
			jo.put("time", vs);
			josns.add(jo);
			xl.put("value", josns);
			xl.put("type", type);
		}

		return xl;
	}

	public class StationWithMsgs {
		private Substation station;
		private String specialSt;// 是否是特维站点
		private Map<String, List<AlarmMessage>> msgs;

		public Substation getStation() {
			return station;
		}

		public void setStation(Substation station) {
			this.station = station;
		}

		public Map<String, List<AlarmMessage>> getMsgs() {
			return msgs;
		}

		public void setMsgs(Map<String, List<AlarmMessage>> msgs) {
			this.msgs = msgs;
		}

		public String getSpecialSt() {
			return specialSt;
		}

		public void setSpecialSt(String specialSt) {
			this.specialSt = specialSt;
		}

	}

	private String initAlarmMesg(List<AlarmMessage> mess) {
		// 获取所有的站点
		Map<String, Substation> allStations = cc.getStationid_map();
		// 获取特维站
		List<String> specSt = cc.getSpecialStationId();

		Map<String, List<AlarmMessage>> alarms = new HashMap<>();
		List<StationWithMsgs> stations = new ArrayList<>();
		outer: for (AlarmMessage m : mess) {
			// String name = RiskTypeEnum.getRiskNameByType(m.getMsgType());
			String name = pc.get(m.getMsgType()).getName();
			String msgContent = m.getContent();
			String stationName = null;
			Substation station = null;
			if (msgContent.contains("/")) {
				stationName = msgContent.split("/")[0];// 根据消息内容截取
			}
			if (stationName != null) {
				// 根据站名获取站 不再查询数据库
				station = getSubStationByName(stationName, allStations);
			} else {
				continue outer;
			}
			List<AlarmMessage> as = alarms.get(name);
			if (as == null) {
				as = new ArrayList<>();
				alarms.put(name, as);
			}
			as.add(m);
			/*
			 * AffectedObject a=null; if(m.getMsgType()!=null&&m.getSource()!=null){ a =
			 * ram.getAffectedObject(m.getMsgType(), m.getSource()); }
			 */
			if (station != null) {
				for (StationWithMsgs sm : stations) {
					if (sm.getStation() != null && (sm.getStation().getId()).equals(station.getId())) {
						Map<String, List<AlarmMessage>> msgs = sm.getMsgs();
						List<AlarmMessage> msg = msgs.get(name);
						if (msg == null) {
							msg = new ArrayList<>();
							msg.add(m);
							msgs.put(name, msg);
							sm.setMsgs(msgs);
							continue outer;
						}
						msg.add(m);
						sm.setMsgs(msgs);
						continue outer;
					}
				}
				StationWithMsgs newsm = new StationWithMsgs();
				Map<String, List<AlarmMessage>> msgs = new HashMap<>();
				List<AlarmMessage> msg = new ArrayList<>();
				msg.add(m);
				msgs.put(name, msg);
				newsm.setMsgs(msgs);
				newsm.setStation(station);
				if (specSt.contains(station.getId())) {
					newsm.setSpecialSt("1");
				} else {
					newsm.setSpecialSt("0");
				}
				stations.add(newsm);
			}
		}
		net.sf.json.JSONObject res = new net.sf.json.JSONObject();
		res.put("alarms", alarms);
		res.put("stations", stations);
		return res.toString();
	}

	private Substation getSubStationByName(String name, Map<String, Substation> stations) {
		for (Map.Entry<String, Substation> s : stations.entrySet()) {
			if (name.equals(s.getValue().getName())) {
				return s.getValue();
			}
		}
		return null;
	}

	/**
	 * 获取一次设备风险告警消息
	 * 
	 * @return
	 */

	@GetMapping("/getPrimaryRiskAlarmMessage")
	public String getPrimaryRiskAlarmMessage() {
		List<AlarmMessage> mess = ram.getPrimaryAlarmMessage();
		sortListByTime(mess);
		return initAlarmMesg(mess);
	}

	/**
	 * 根据时间获取一次设备风险告警消息
	 * 
	 * @return
	 */

	@GetMapping("/getPrimaryRiskAlarmMessageByTime/{start}/{end}")
	public String getPrimaryRiskAlarmMessageByTime(@PathVariable("start") String start, @PathVariable("end") String end) {
		List<AlarmMessage> mess = ram.getPrimaryAlarmMessageByTime(start, end);
		sortListByTime(mess);
		JSONObject result = new JSONObject();
		result.put("alarms", mess);
		return result.toString();
	}

	/**
	 * 获取二次设备风险告警消息
	 * 
	 * @return
	 */

	@GetMapping("/getSecondaryAlarmMessage")
	public String getSecondaryAlarmMessage() {
		List<AlarmMessage> mess = ram.getSecondaryAlarmMessage();
		sortListByTime(mess);
		return initAlarmMesg(mess);
	}

	/**
	 * 根据时间获取二次设备风险告警消息
	 * 
	 * @return
	 */

	@GetMapping("/getSecondaryAlarmMessageByTime/{start}/{end}")
	public String getSecondaryAlarmMessageByTime(@PathVariable("start") String start, @PathVariable("end") String end) {
		List<AlarmMessage> mess = ram.getSecondaryAlarmMessageByTime(start, end);
		sortListByTime(mess);
		JSONObject result = new JSONObject();
		result.put("alarms", mess);
		return result.toString();
	}

	/**
	 * 根据条件查询告警消息
	 * @param msgType
	 * @param startTime
	 * @param endTime
	 * @param stationName
	 * @return
	 */

	@GetMapping("/getAlarmMessageByConditions/{msgType}/{startTime}/{endTime}/{stationName}")
	public String getAlarmMessageByConditions(@PathVariable("msgType") String msgType,
			@PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime,
			@PathVariable("stationName") String stationName) {
		Date start = DateUtil.getDateByString(startTime, DateUtil.pattern_ymd);
		Date end = DateUtil.getDateByString(endTime, DateUtil.pattern_ymd);
		stationName = "null".equals(stationName) ? null : stationName;
		List<AlarmMessage> mess = ram.getAlarmMessageByTypeStationDate(msgType, stationName, start, end);
		return initAlarmMesgSearchResult(mess);
	}

	private String initAlarmMesgSearchResult(List<AlarmMessage> mess) {
		Map<String, List<AlarmMessage>> alarms = new HashMap<>();
		for (AlarmMessage m : mess) {
			String name = pc.get(m.getMsgType()).getName();
			List<AlarmMessage> as = alarms.get(name);
			if (as == null) {
				as = new ArrayList<>();
				alarms.put(name, as);
			}
			as.add(m);
		}
		return net.sf.json.JSONObject.fromObject(alarms).toString();
	}

	/**
	 * 获取所有的消息类型
	 * @return
	 */

	@GetMapping("/getAllAlarmType")
	public String getAllAlarmType() {
		JSONObject jso = new JSONObject();
		Set<SystemParameter> pps = getAlarmTypes();
		for (SystemParameter p : pps) {
			jso.put(p.getName(), p.getId());
		}

		return jso.toJSONString();
	}

	private Set<SystemParameter> getAlarmTypes() {
		Set<SystemParameter> rtns = new HashSet<>();
		String pushmsgs = pc.get(ParamDesc.RISK_ALARM_TYPES).getValue();
		if (StringUtils.isNoneEmpty(pushmsgs)) {
			String[] ids = pushmsgs.split(",");
			for (int i = 0; i < ids.length; i++) {
				rtns.add(pc.get(ids[i]));
			}
		}
		return rtns;
	}

	private void sortListByTime(List<AlarmMessage> msgList) {
		Collections.sort(msgList, new Comparator<AlarmMessage>() {
			@Override
			public int compare(AlarmMessage o1, AlarmMessage o2) {
				return o2.getCreateTime().compareTo(o1.getCreateTime());
			}

		});
	}


	@GetMapping("/getRiskAlarmCount")
	public String getRiskAlarmCount() {
		JSONArray result = new JSONArray();
		result.add(ram.getPrimaryAlarmCount());
		result.add(ram.getSecondaryAlarmCount());
		return result.toJSONString();
	}


	@GetMapping("/getAffectedObjectByTypeAndOid/{id}/{type}/{oid}")
	public String getAffectedObjectByTypeAndOid(@PathVariable("id") String id, @PathVariable("type") String type,
												@PathVariable("oid") String oid) {
		AffectedObject ao = ram.getAffectedObject(type, oid);
		/*
		 * if (ao == null) { dao.delete(id, AlarmMessage.class); return null; }
		 */
		return net.sf.json.JSONObject.fromObject(ao).toString();
	}
}
